package com.itcast.donkey.adapters;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.activity.ScenicSpotActivity;
import com.itcast.donkey.modles.AttackModles;
import com.itcast.donkey.modles.RecyclerGridView;


public class AttackAdapter extends RecyclerView.Adapter<AttackAdapter.AttackViewHolder> {


    private Context context;
    private AttackModles asiaModles;
    private AttackModles europeModles;
    private AttackModles americeModles;
    private AttackModles hongkongModles;
    private AttackModles continentModles;
    private GridViewAttackAdapter adapter;
    private int count;

    private SharedPreferences sp;
    private String areaName;
    private int id;
    private boolean isNull;

    public AttackAdapter(Context context, AttackModles asiaModles, AttackModles europeModles, AttackModles americeModles, AttackModles hongkongModles, AttackModles continentModles) {
        this.context = context;
        this.asiaModles = asiaModles;
        this.europeModles = europeModles;
        this.americeModles = americeModles;
        this.hongkongModles = hongkongModles;
        this.continentModles = continentModles;
        this.count = asiaModles.getDestinations().size() + europeModles.getDestinations().size()
                + americeModles.getDestinations().size() + hongkongModles.getDestinations().size() + continentModles.getDestinations().size();

        sp = context.getSharedPreferences("attackArea", Context.MODE_PRIVATE);
        this.areaName = sp.getString("areaName", "日本");
        this.id = sp.getInt("id", 55);
        this.isNull = sp.getBoolean("isNull", true);

    }

    @Override
    public AttackViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_attack, null);
        AttackViewHolder holder = new AttackViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final AttackViewHolder holder, int position) {

        if (position == 0) {

            if (isNull) {
                holder.linAreaName.setVisibility(View.GONE);
            } else {
                holder.linAreaName.setVisibility(View.VISIBLE);
                holder.tvAreaName.setText(areaName);

                holder.tvAreaName.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, ScenicSpotActivity.class);
                        intent.putExtra("id", id);
                        context.startActivity(intent);
                    }
                });
            }
            holder.tv_title.setText("国外.亚洲");
            adapter = new GridViewAttackAdapter(context, asiaModles);
            holder.gv_content.setAdapter(adapter);
            //第一个Item
            holder.gv_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(context, ScenicSpotActivity.class);
                    intent.putExtra("id", asiaModles.getDestinations().get(position).getId());
                    intent.putExtra("title", asiaModles.getDestinations().get(position).getName_zh_cn());
                    context.startActivity(intent);
                }
            });
        }
        if (position == 1) {
            holder.tv_title.setText("国外.欧洲");
            adapter = new GridViewAttackAdapter(context, europeModles);
            holder.gv_content.setAdapter(adapter);
            //点击第二个Item
            holder.gv_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(context, ScenicSpotActivity.class);
                    intent.putExtra("id", europeModles.getDestinations().get(position).getId());
                    intent.putExtra("title", europeModles.getDestinations().get(position).getName_zh_cn());
                    context.startActivity(intent);
                }
            });
        }
        if (position == 2) {
            holder.tv_title.setText("美洲.大洋洲.非洲与南极洲");
            adapter = new GridViewAttackAdapter(context, americeModles);
            holder.gv_content.setAdapter(adapter);
            //点击第三个Item
            holder.gv_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(context, ScenicSpotActivity.class);
                    intent.putExtra("id", americeModles.getDestinations().get(position).getId());
                    intent.putExtra("title", americeModles.getDestinations().get(position).getName_zh_cn());
                    context.startActivity(intent);
                }
            });
        }
        if (position == 3) {
            holder.tv_title.setText("国内.港澳台");
            adapter = new GridViewAttackAdapter(context, hongkongModles);
            holder.gv_content.setAdapter(adapter);
            //第四个Item
            holder.gv_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(context, ScenicSpotActivity.class);
                    intent.putExtra("id", hongkongModles.getDestinations().get(position).getId());
                    intent.putExtra("title", hongkongModles.getDestinations().get(position).getName_zh_cn());
                    context.startActivity(intent);
                }
            });

        }
        if (position == 4) {
            holder.tv_title.setText("国内.大陆");
            adapter = new GridViewAttackAdapter(context, continentModles);
            holder.gv_content.setAdapter(adapter);
            //第五个Item
            holder.gv_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(context, ScenicSpotActivity.class);
                    intent.putExtra("id", continentModles.getDestinations().get(position).getId());
                    intent.putExtra("title", continentModles.getDestinations().get(position).getName_zh_cn());
                    context.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return 5;
    }

    public class AttackViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_title;
        private RecyclerGridView gv_content;
        private LinearLayout linAreaName;
        private TextView tvAreaName;


        public AttackViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_area);
            gv_content = (RecyclerGridView) itemView.findViewById(R.id.gridView_fragment_attack);

            linAreaName = (LinearLayout) itemView.findViewById(R.id.lin_area_name);
            tvAreaName = (TextView) itemView.findViewById(R.id.tv_area_name);

        }
    }
}
